﻿using CommonCater;
using ItcastCater;
using Lufei_CaterBLL;
using Lufei_CaterModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lufei_CaterUI
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();

            log<FrmMain>.LogDebug("FrmMain启动完成");
        }

        private void btnMemmber_Click(object sender, EventArgs e)
        {
            //创建会员管理页面
            FrmMemmberInfo frmMemmber = new FrmMemmberInfo();
            frmMemmber.Show();//使用show FrmMemmberInfo_Load

        }

        //商品管理
        private void button5_Click(object sender, EventArgs e)
        {
            FrmCategory frmCategory = new FrmCategory();
            frmCategory.Show();
        }


        /// <summary>
        /// 加载房间方法
        /// </summary>
        public void LoadRooms()
        {
            //1.给tabcontrol添加TabPage(TabPage数量是根据room的名字来定义)
            //2.读取数据库
            //3.通过循环去添加
            RoomInfoBLL bll = new RoomInfoBLL();
            //获取房间集合
            List<RoomInfo> listRooms=   bll.GetRoomsByDelFlag();

            for (int i = 0; i < listRooms.Count; i++)
            {
                //winform里面创建空间2种方式
                //1.拖控件（通过反射去生成代码）
                //2.写代码 (创建空间对象)
                TabPage tp = new TabPage();//创建tabpage
                //tp显示的文字
                //把RoomName属性赋值给TabPage
                tp.Text = listRooms[i].RoomName;
                //强绑定   给TabPage和房间对象做一个强绑定
                //Tag:给程序元看的属性
                tp.Tag = listRooms[i];
                //每一个tabpage都要一个listview
                ListView listView = new ListView();
                //填充满容器
                listView.Dock=DockStyle.Fill;
                listView.MultiSelect = false;//不允许多选
                listView.View = View.LargeIcon;//大图片显示
                //绑定最图片
                listView.LargeImageList = imageList1;
                //给每一个TabPage添加一个ListView对象
                tp.Controls.Add(listView);
                //把tp添加到tbcontrol里面
                tabc.TabPages.Add(tp);

            }
        }

        //1.注册load事件
        private void FrmMain_Load(object sender, EventArgs e)
        {
            LoadRooms();
            //SelectedTab当前被选中的tabpage
            LoadDeskByRoomID(tabc.SelectedTab);
        }


        /// <summary>
        /// 加载桌子
        /// </summary>
        private void LoadDeskByRoomID(TabPage tp)
        {
            //房间ID
            //根据房间ID 查询该房间下所有的桌子  房间ID？listRooms ----->当前的房间  -----》当前选择的TabPage
            //拿到房间对象
            RoomInfo ri= tp.Tag as RoomInfo;
            //拿到房间id
            int rID=  ri.RoomId;
           
            //创建业务逻辑层
            DeskInfoBLL bll = new DeskInfoBLL();
            //获取所有的桌子
            List<DeskInfo> listDesks=  bll.GetDesksByRoomID(rID);

            //获取ListView
            ListView list = tp.Controls[0] as ListView;
            //清除ListView中已有的数据
            list.Items.Clear();

            //把所有获取到的桌子放在每一个TabPage下面的ListView上
            for (int i = 0; i < listDesks.Count; i++)
            {
                //桌子放在集合上
                // listDesks[i].DeskName显示文本
                // listDesks[i].DeskState 图片显示的样子
                list.Items.Add(listDesks[i].DeskName, listDesks[i].DeskState);

                //把桌子对象和ListView进行强绑定
                list.Items[i].Tag = listDesks[i];
            }
        }



        /// <summary>
        /// 每次切换TabPage的时候 都会调用  调用LoadDeskByRoomID();
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabc_SelectedIndexChanged(object sender, EventArgs e)
        {
            //每次切换TabPage的时候 都会调用  调用LoadDeskByRoomID();
            LoadDeskByRoomID(tabc.SelectedTab);
        }

        //创建事件
        private event EventHandler my_Event_Billing;

        /// <summary>
        /// 顾客开单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBilling_Click(object sender, EventArgs e)
        {

            //创建开单窗体
            FrmBilling frmBilling = new FrmBilling();
            //1.判断当前房间是否有桌子被选择（房间----》tabPage）
            //拿到桌子
             ListView listview=  tabc.SelectedTab.Controls[0] as ListView;
            if (listview.SelectedItems.Count<=0)
            {
                MessageBox.Show("请选择桌子后在开单!!!!!!!","警告");
                log<FrmMain>.LogWarn("没有选择桌子");
                return;
            }

            //2.判断选中桌子是否是空闲状态
            //listview设置的是单选 所有他选择的只能是一个 第一个索引
            DeskInfo deskInfo= listview.SelectedItems[0].Tag as DeskInfo;

            if (deskInfo.DeskState!=0)
            {
                MessageBox.Show("当前桌子已经开单!!!!!!!,请选择其他为开单的桌子!!!!!!!");
                log<FrmMain>.LogWarn("当前桌子已经开单");
                return;
            }
            //窗体传值
            //1.餐厅的名字  2.餐厅的id  3.房间的类型  4.房间最低消费
            UpdateMemmberInfo<RoomInfo> u = new UpdateMemmberInfo<RoomInfo>();
            u.Type = deskInfo.DeskId;//拿到id
            u.Name = deskInfo.DeskName;//名字
            u.obj = tabc.SelectedTab.Tag as RoomInfo;//拿到房间信息
            this.my_Event_Billing += frmBilling.SetValue;
            if (this.my_Event_Billing!=null)
            {
                //调用事件
                this.my_Event_Billing(this,u);
            }
            //注册关闭事件
            frmBilling.FormClosed += FrmBilling_FormClosed;
            frmBilling.ShowDialog();
        }

        private void FrmBilling_FormClosed(object sender, FormClosedEventArgs e)
        {
            //当关闭窗体的时候 刷新桌子的状态
            LoadDeskByRoomID(tabc.SelectedTab);
        }

        /// <summary>
        /// 打开房间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            FrmRoom room = new FrmRoom();
            room.ShowDialog();
        }


        OrderInfoBLL orderInfoBLL = new OrderInfoBLL();

        //定义打开订单的事件AddMoney
        private event EventHandler My_Event_AddMoney;

        /// <summary>
        /// 打开点单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMoney_Click(object sender, EventArgs e)
        {
            //1.判断是否有选中桌子
            //桌子==》ListView ==>TabPage  ===？TablControl
            ListView listView= tabc.SelectedTab.Controls[0] as ListView;
            if (listView.SelectedItems.Count<=0)
            {
                MessageBox.Show("请选择要添加消费的桌子!!!");
                log<FrmMain>.LogError("请选择要添加消费的桌子!!!");
                return;
            }
            //2.选中的桌子必须是开单状态
            //2.1获取桌子  2.或者桌子的状态进行判断
            DeskInfo deskInfo= listView.SelectedItems[0].Tag as DeskInfo;
            if (deskInfo.DeskState!=1)
            {
                MessageBox.Show("请先给当前桌子进行开单，再添加消费!!!!");
                log<FrmMain>.LogError("请先给当前桌子进行开单，再添加消费!!!!");
                return;
            }
            //选中了桌子也是开单状态
            FrmAddMoney frmAdd=new FrmAddMoney();
            //添加关闭事件
            frmAdd.FormClosed += FrmAdd_FormClosed;
            UpdateMemmberInfo<FrmBilling> ui = new UpdateMemmberInfo<FrmBilling>();

            //下面需要传递桌子编号和   OrderID
            //通过桌子id去查询 OrderID
            int orderId=  orderInfoBLL.GetOrderIDByDeskId(deskInfo.DeskId);
            ui.Name = deskInfo.DeskName;
            ui.Type = orderId;
            this.My_Event_AddMoney += frmAdd.SetValue;
            if (this.My_Event_AddMoney!=null)
            {
                this.My_Event_AddMoney(this, ui);
            }
            frmAdd.ShowDialog();
        }


        /// <summary>
        /// FrmAdd关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void FrmAdd_FormClosed(object sender, FormClosedEventArgs e)
        {
            //throw new NotImplementedException();
           // MessageBox.Show("点菜页面被关闭了");

        }



        /// <summary>
        /// 弹出结账窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnJieZhang_Click(object sender, EventArgs e)
        {
            //1.判断是否有选中桌子
            //桌子==》ListView ==>TabPage  ===？TablControl
            ListView listView = tabc.SelectedTab.Controls[0] as ListView;
            if (listView.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请选择要结账的桌子!!!");
                log<FrmMain>.LogError("请选择要结账的桌子!!!");
                return;
            }
            //2.选中的桌子必须是开单状态
            //2.1获取桌子  2.或者桌子的状态进行判断
            DeskInfo deskInfo = listView.SelectedItems[0].Tag as DeskInfo;

            if (deskInfo.DeskState != 1)
            {
                MessageBox.Show("请先给当前桌子进行开单，再结账!!!!");
                log<FrmMain>.LogError("请先给当前桌子进行开单，再结账!!!!");
                return;
            }


            //在FrmBalance里面创建2个属性存储值
            //弹出结账窗体
            FrmBalance frmBalance = new FrmBalance();
            //获取OrderId
            int orderID=  orderInfoBLL.GetOrderIDByDeskId(deskInfo.DeskId);
            //获取桌子编号
            string deskName = deskInfo.DeskName;
            frmBalance.OrderID = orderID;
            frmBalance.DeskName = deskName;
            frmBalance.DeskID = deskInfo.DeskId;
            frmBalance.FormClosed += FrmBalance_FormClosed;
            frmBalance.ShowDialog();
        }

        private void FrmBalance_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadDeskByRoomID(tabc.SelectedTab);
        }
    }
}
